/**
* 基础菜单 商品管理
*/
<template>
	<div>
		<!-- 面包屑导航 -->
		<el-breadcrumb separator-class="el-icon-arrow-right">
			<el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
			<el-breadcrumb-item>资金账户</el-breadcrumb-item>
		</el-breadcrumb>
		<!-- 搜索筛选 -->
		<el-form :inline="true" :model="formInline" class="user-search">
		</el-form>
		<!--列表-->
		<el-table size="small" :data="listData" highlight-current-row v-loading="loading" border
			element-loading-text="拼命加载中" style="width: 90%;">
			<el-table-column align="center" type="selection" width="60">
			</el-table-column>
			<el-table-column sortable prop="uaccount" label="用户账号" width="150">
			</el-table-column>
			<el-table-column sortable prop="ename" label="企业名称" width="150">
			</el-table-column>
			<el-table-column sortable prop="faccount" label="资金账户编号" width="150">
			</el-table-column>
			<el-table-column sortable prop="residual" label="账户余额" width="150">
			</el-table-column>
			<el-table-column sortable prop="status" label="账户状态" width="150">
			</el-table-column>
			<el-table-column align="center" label="操作" min-width="100">
				<template slot-scope="scope">
					<el-button size="mini" @click="handleEdit(scope.$index, scope.row)">审核激活</el-button>
					<el-button size="mini" type="danger" @click="deleteUser(scope.$index, scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>
		<!-- 分页组件 -->
		<Pagination v-bind:child-msg="pageparm" @callFather="callFather"></Pagination>
		<!-- 编辑界面 -->
		<el-dialog :title="title" :visible.sync="editFormVisible" width="30%" @click="closeDialog">
			<el-form label-width="120px" :model="editForm" :rules="rules" ref="editForm">
				<el-form-item label="用户账号" prop="uaccount">
					<el-input size="small" v-model="editForm.uaccount" auto-complete="off" placeholder="用户账号"
						readonly>
					</el-input>
				</el-form-item>
				<el-form-item label="企业名称" prop="ename">
					<el-input size="small" v-model="editForm.ename" auto-complete="off" placeholder="企业名称"
						readonly>
					</el-input>
				</el-form-item>
				<el-form-item label="status" prop="type">
					<el-select v-model="editForm.status" placeholder="status">
						<el-option v-for="item in Options" :key="item.value" :label="item.label"
							:value="item.value">
						</el-option>
					</el-select>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button size="small" @click="closeDialog">取消</el-button>
				<el-button size="small" type="primary" :loading="loading" class="title" @click="submitForm('editForm')">
					确定</el-button>
			</div>
		</el-dialog>
	</div>
</template>

<script>
	import {
		deptList,
		deptSave,
		deptDelete
	} from '../../api/userMG'
	import Pagination from '../../components/Pagination'
	import axios from '../../api/axiosFun.js'
	export default {
		data() {
			return {
				//上传的文件
				dialogVisible: false, //添加页面
				nshow: true, //switch开启
				fshow: false, //switch关闭
				loading: false, //是显示加载
				editFormVisible: false, //控制编辑页面显示与隐藏
				restaurants: [],
				title: '添加',
				Options: [{
						value: "拒绝",
						label: "拒绝",
					},
					{
						value: "同意",
						label: "同意",
					},
				],
				editForm: {
					id: '',
					uaccount: '',
					ename: '',
					residual: '',
					status: '',
					faccount: '',
					pay_pass: '',
					newpay_pass: '',
					changepay_pass: '',
					token: localStorage.getItem('logintoken')
				},
				// rules表单验证
				rules: {
					deptName: [{
						required: true,
						message: '请输入部门名称',
						trigger: 'blur'
					}],
					deptNo: [{
						required: true,
						message: '请输入部门代码',
						trigger: 'blur'
					}]
				},
				formInline: {
					page: 1,
					limit: 8,
					varLable: '',
					varName: '',
					token: localStorage.getItem('logintoken'),
					user: JSON.parse(localStorage.getItem("userdata")),
				},
				// 删除部门
				seletedata: {
					ids: '',
					token: localStorage.getItem('logintoken')
				},
				userparm: [], //搜索权限
				listData: [], //用户数据
				// 分页参数
				pageparm: {
					currentPage: 1,
					pageSize: 5,
					total: 10
				}
			}
		},
		// 注册组件
		components: {
			Pagination
		},
		/**
		 * 数据发生改变
		 */

		/**
		 * 创建完毕
		 */
		created() {
			this.getdata()
			//如果roleid为1 表示是平台管理员 可显示所有企业的资金账户
		},
		/**
		 * 里面的方法只有被调用才会执行
		 */
		methods: {
			//文件上传
			submitUpload() {
				this.$refs.upload.submit();
			},
			handleRemove(file, fileList) {
				console.log(file, fileList);
			},
			handlePreview(file) {
				console.log(file);
			},
			//联想搜索
			querySearch(queryString, cb) {
				var restaurants = this.restaurants;
				var results = queryString ? restaurants.filter(this.createFilter(queryString)) : restaurants;
				// 调用 callback 返回建议列表的数据
				cb(results);
			},
			createFilter(queryString) {
				return (restaurant) => {
					return (restaurant.value.toLowerCase().indexOf(queryString.toLowerCase()) === 0);
				};
			},

			handleSelect(item) {
				console.log(item);
			},


			// 获取公司列表
			getdata() {
				this.loading = true
				/***
				 * 调用接口，注释上面模拟数据 取消下面注释
				 */
				/* this.$axios.get('/api/etmsUser/findStudentByTrain/' + this.formInline.currentPage + '/' + this.formInline
						.pageSize + '/' + this.tid)*/
				this.$axios.post("api/faccount/findAll/" + this.formInline.page + "/" + this.formInline.limit)
					.then(res => {
						this.loading = false
						console.log(res)
						if (res.data.code == 500) {
							this.$message({
								type: 'info',
								message: res.data.msg
							})
						} else {
							this.listData = res.data.data.list
							// 分页赋值
							this.pageparm.currentPage = res.data.data.pageNum
							this.pageparm.pageSize = res.data.data.pageSize
							this.pageparm.total = res.data.data.total

						}
					})
					.catch(err => {
						this.loading = false
						this.$message.error('菜单加载失败，请稍后再试！')
					})
			},
			// 分页插件事件 

			callFather(parm) {
				this.formInline.page = parm.currentPage
				this.formInline.limit = parm.pageSize
				this.getdata(this.formInline)
			},
			// 显示添加
			details: function() {
				this.dialogVisible = true
				this.title = '资金账户添加'


			},
			//显示编辑界面
			handleEdit: function(index, row) {
				this.editFormVisible = true
				if (row != undefined && row != 'undefined') {
					this.title = '激活'
					this.editForm.ename=row.ename
					this.editForm.uaccount=row.uaccount
					this.editForm.status=row.status
					this.editForm.id = row.id
				} else {
					this.title = '注册资金账户'
				}

			},
			submitForm(editData) {
				console.log(this.editForm.status)
				if(this.editForm.status!='同意'){
					this.$axios.post("api/faccount/freeze/" +this.editForm.id).then(res => {
						this.editFormVisible = false
						this.loading = false
						if (res.data.code == 200) {
							this.getdata()
							this.$message({
								type: 'success',
								message: '冻结成功！'
							})
						} else {
							this.$message({
								type: 'info',
								message: res.data.msg
							})
						}
					}).catch(err => {
						this.editFormVisible = false
						this.loading = false
						this.$message.error('冻结失败，请稍后再试！')
					})
				}else{
					this.$axios.post("api/faccount/activation/" +this.formInline.user.enterprise_id).then(res => {
						this.editFormVisible = false
						this.loading = false
						if (res.data.code == 200) {
							this.getdata()
							this.$message({
								type: 'success',
								message: '修改成功！'
							})
						} else {
							this.$message({
								type: 'info',
								message: res.data.msg
							})
						}
					}).catch(err => {
						this.editFormVisible = false
						this.loading = false
						this.$message.error('修改失败，请稍后再试！')
					})
				}
						
			},
			// 删除公司
			deleteUser(index, row) {
				this.$confirm('确定要删除吗?', '信息', {
						confirmButtonText: '确定',
						cancelButtonText: '取消',
						type: 'warning'
					})
					.then(() => {
						this.$axios.post("api/faccount/del/" + row.id)
							.then(res => {
								if (res.data.code == 200) {
									this.$message({
										type: 'success',
										message: '已删除!'
									})
									this.getdata()
								} else {
									this.$message({
										type: 'info',
										message: res.data.msg
									})
								}
							})
							.catch(err => {
								this.loading = false
								this.$message.error('删除失败，请稍后再试！')
							})
					})
					.catch(() => {
						this.$message({
							type: 'info',
							message: '已取消删除'
						})
					})
			},
			// 关闭编辑、增加弹出框
			closeDialog() {
				this.editFormVisible = false
			}
		}
	}
</script>

<style scoped>
	.user-search {
		margin-top: 20px;
	}

	.userRole {
		width: 100%;
	}
</style>
